/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.server.scope;

import java.io.Serializable;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.daikit.commons.shared.utils.DkIdGenerator;
import com.daikit.commons.shared.utils.DkObjectUtils;
import com.daikit.daikit4gxtsample.shared.bean.User;


/**
 * Container for variables that should be stored in a Session scope.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
@Component
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionParametersContainer implements Serializable
{

	private static final long serialVersionUID = -6435654912909170842L;

	protected Log log = LogFactory.getLog(getClass());

	private final String id;

	private boolean sessionLogged = false;

	private User loggedUser;
	private String loggedUserSessionSid;
	private Locale currentLocale;

	/**
	 * Default constructor.
	 */
	public SessionParametersContainer()
	{
		this.id = DkIdGenerator.generateRandomId(8);
		this.sessionLogged = false;
		this.loggedUser = null;
		this.currentLocale = null;
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Clear session parameters
	 */
	public void clear()
	{
		loggedUser = null;
		loggedUserSessionSid = null;
		currentLocale = null;
	}

	/**
	 * @param newLoggedUser
	 *           the user to test if the same than the session one.
	 * @return whether the current logged user is the same as the given one.
	 */
	public boolean sessionUserHasChanged(final User newLoggedUser)
	{
		return !DkObjectUtils.equalsWithNull(getLoggedUser(), newLoggedUser);
	}

	/**
	 * Update session user and session id.
	 * 
	 * @param loggedUser
	 * @param loggedUserSessionSid
	 */
	public void update(final User loggedUser, final String loggedUserSessionSid)
	{
		setLoggedUser(loggedUser);
		setLoggedUserSessionSid(loggedUserSessionSid);
		log.debug("[" + id + "] Update with u=" + loggedUser + " sid=" + loggedUserSessionSid);
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// GETTERS / SETTERS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * 
	 * @return the session ID.
	 */
	public String getId()
	{
		return id;
	}

	/**
	 * @return whether the session was logged in database
	 */
	public boolean isSessionLogged()
	{
		return sessionLogged;
	}

	/**
	 * Update sessionLogged attribute
	 * 
	 * @param sessionLogged
	 *           whether the session was logged in database
	 */
	public void setSessionLogged(final boolean sessionLogged)
	{
		this.sessionLogged = sessionLogged;
	}

	/**
	 * @return the logged user session id
	 */
	public String getLoggedUserSessionSid()
	{
		return loggedUserSessionSid;
	}

	/**
	 * Set the logged user session id
	 * 
	 * @param loggedUserSessionSid
	 */
	public void setLoggedUserSessionSid(final String loggedUserSessionSid)
	{
		this.loggedUserSessionSid = loggedUserSessionSid;
	}

	/**
	 * @return the loggedUser
	 */
	public User getLoggedUser()
	{
		return loggedUser;
	}

	/**
	 * @param loggedUser
	 *           the loggedUser to set
	 */
	public void setLoggedUser(final User loggedUser)
	{
		this.loggedUser = loggedUser;
	}

	/**
	 * @return the currentLocale
	 */
	public Locale getCurrentLocale()
	{
		return currentLocale;
	}

	/**
	 * @param currentLocale
	 *           the currentLocale to set
	 */
	public void setCurrentLocale(final Locale currentLocale)
	{
		this.currentLocale = currentLocale;
	}
}
